//
//  XLDataViewController.m
//  XLPersonCenter
//
//  Created by 宋振 on 2018/7/23.
//  Copyright © 2018年 夜幕繁华. All rights reserved.
//

#import "XLDataViewController.h"

#import "XLUserDataCell.h"
#import "XLUserEvaluateCell.h"

#import "XLPPGuardianListView.h"
#import "XLUserDataHeaderView.h"

#import "XLGuardianListViewController.h"
#import "XLGiftViewController.h"

@interface XLDataViewController ()<UITableViewDelegate,UITableViewDataSource,XLPPGuardianListViewDelegate,XLUserDataHeaderViewDelegate>

/**
 容器
 */
@property (nonatomic, strong) UITableView *  tableView;

//数据信息
@property (nonatomic, strong) NSMutableArray *  dataSources;

//守护榜
@property (nonatomic, strong) XLPPGuardianListView *  guardianListView;

//礼物榜
@property (nonatomic, strong) XLUserDataHeaderView *userDataHeader;

@end

//个人信息
static NSString *const userDataCell = @"xlUserDataCell";

//用户评价
static NSString *const userEvaluateCell = @"xlUserEvaluateCell";



@implementation XLDataViewController
{
    CGFloat cellHeight;
    
    //数据个数
    NSInteger dataSourceCount;
    
    //是否添加守护榜
    BOOL isGurandian;
    
    //是否添加礼物榜
    BOOL isGift;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    /**
     初始化数据信息
     */
    [self initSource];
    
    /**
     设置视图显示
     */
    [self setCustomeView];
    
    
}

#pragma mark    -   -   -   初始化数据信息 -   -   -
- (void)initSource
{
    cellHeight = 136;
    
    dataSourceCount = 1;
    
    isGurandian = NO;
    isGift = NO;
    
    NSLog(@"UserInfoModel:   %@",self.userInfoModel);
    NSLog(@"==========================");
    
    if (self.userInfoModel.CommentList.count > 0) {
        dataSourceCount ++;
    }
    
    if (self.userInfoModel.GiftList.count > 0) {
        isGift = YES;
    }
    
    if (self.userInfoModel.KnightList.count > 0) {
        isGurandian = YES;
    }
    
//    [self.tableView reloadData];
}

#pragma mark    -   -   - 设置视图显示 -  -   -
- (void)setCustomeView
{
    
    
    //设置容器显示
    [self layOutTableView];
}

/**
 设置容器界面
 */
- (void)layOutTableView
{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat naviHeight = IS_IPhoneX ? 88.0:64.0;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-106-25) style:(UITableViewStyleGrouped)];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    [self.view addSubview:self.tableView];

    
    //注册cell
    [self registCell];
}

/**
 注册cell
 */
- (void)registCell
{
    [self.tableView registerClass:[XLUserDataCell class] forCellReuseIdentifier:userDataCell];
    [self.tableView registerClass:[XLUserEvaluateCell class] forCellReuseIdentifier:userEvaluateCell];
}




#pragma mark    - - - UITableViewDelegate,UITableViewDataSource - - -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSourceCount;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    
    
    if (isGurandian) {
        //守护榜
        self.guardianListView = [[XLPPGuardianListView alloc] initWithFrame:CGRectZero];
        
        self.guardianListView.delegate = self;
        
        self.guardianListView.nightArray = self.userInfoModel.KnightList;
        
        [view addSubview:self.guardianListView];
        
        [self.guardianListView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.right.equalTo(view);
            make.height.mas_equalTo(114);
        }];
    }
    
    
    
    
    //礼物榜
    if (isGift) {
        
        self.userDataHeader = [[XLUserDataHeaderView alloc] init];
        
        self.userDataHeader.dataArray = [self.userInfoModel.GiftList mutableCopy];
        
        self.userDataHeader.delegate = self;
        
        [view addSubview:self.userDataHeader];
        
        [self.userDataHeader mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (isGurandian) {
                make.top.equalTo(self.guardianListView.mas_bottom);
            }else{
                make.top.equalTo(view.mas_top);
            }
            make.left.right.equalTo(view);
            make.bottom.equalTo(view.mas_bottom);
        }];
        
    }
    
    return view;
}

//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    return [UIView new];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (isGurandian && isGift) {
        return 228;
    }else if (isGurandian || isGift)
    {
        return 114;
    }else{
        return 0.1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        
        //个人信息
        
        XLUserDataCell *cell = [tableView dequeueReusableCellWithIdentifier:userDataCell];
        if (!cell) {
            cell = [[XLUserDataCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:userDataCell];
        }
        
        cell.userInfoModel = self.userInfoModel;
        
        cellHeight = [cell getCellHeight];
        
        return cell;

    }else{
        
        //用户评价
        
        XLUserEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:userEvaluateCell];
        
        if (!cell) {
            cell = [[XLUserEvaluateCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:userEvaluateCell];
        }
        
        cell.dataSources = [self.userInfoModel.CommentList copy];
        
        cellHeight = [cell getCellHeight];

        return cell;
    }

    
    return [UITableViewCell new];
}

//刷新数据
- (void)setUserInfoModel:(XLUserInfoDetailModel *)userInfoModel
{
    _userInfoModel = userInfoModel;
    
    if (userInfoModel.CommentList.count > 0) {
        dataSourceCount ++;
    }

    if (userInfoModel.GiftList.count > 0) {
        isGift = YES;
    }

    if (userInfoModel.KnightList.count > 0) {
        isGurandian = YES;
    }

    [self.tableView reloadData];
}


#pragma mark    -   -   -Delegate-  -   -

- (void)guardianListViewSelected
{
    XLGuardianListViewController *gurationVc = [[XLGuardianListViewController alloc] init];
    
//    gurationVc.dataArray = self.userInfoModel.KnightList;
    gurationVc.userId = [NSString stringWithFormat:@"%@",self.userInfoModel.UserId];
    
    [self.navigationController pushViewController:gurationVc animated:YES];
}

- (void)guardianListView:(XLPPGuardianListView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"22222222");
}

//跳转界面
- (void)userDataHeaderViewDidSelect
{
    XLGiftViewController *giftController = [[XLGiftViewController alloc] init];
    
    giftController.userId = [NSString stringWithFormat:@"%@",self.userInfoModel.UserId];
    
    [self.navigationController pushViewController:giftController animated:YES];
}

#pragma mark    -   -   -父类方法-  -   -
- (void)scrollViewToTop
{
    [self.tableView setContentOffset:CGPointZero];
}


#pragma mark    ---懒加载---
- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        self.dataSources = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataSources;
}

- (BOOL)prefersStatusBarHidden{
    
    return NO;
    
}



@end
